package com.carkeeper.subject.controller;

import com.carkeeper.common.utils.PageResult;
import com.carkeeper.common.utils.Result;
import com.carkeeper.common.utils.StatusCode;
import com.carkeeper.subject.pojo.Merchant;
import com.carkeeper.subject.pojo.Store;
import com.carkeeper.subject.service.AuditService;
import com.carkeeper.subject.service.StoreService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author chengxihua
 * @version 1.0
 * @date 2020/11/24 10:14
 */
@RestController
@RequestMapping("/audit")
@Api(tags = "审核API")
@CrossOrigin
public class AuditController {
    @Autowired
    private AuditService auditService;

    @Autowired
    private StoreService storeService;
    /**
     * 查询所有未审核状态的信息
     *
     * @return
     */
    @GetMapping("/findByAuditStatus/{page}/{size}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searMap",value = "条件"),
            @ApiImplicitParam(name = "page",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页显示")
    })
    public Result findByAuditStatus(@RequestParam Map searMap,@PathVariable int page,@PathVariable int size) {
        Page<Store> pageList = auditService.findByCommit(searMap,page,size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    /**
     * 查询所有审核失败状态的信息
     *
     * @return
     */
    @GetMapping("/failSearch/{page}/{size}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searMap",value = "条件"),
            @ApiImplicitParam(name = "page",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页显示")
    })
    public Result findByAuditStatusInReject(@RequestParam Map searMap,@PathVariable int page,@PathVariable int size) {
        Page<Store> pageList = auditService.findByAuditStatusInReject(searMap,page,size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }


    /**
     * 分页
     * @param searMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searMap",value = "条件"),
            @ApiImplicitParam(name = "page",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页显示")
    })
    public Result findPage(@RequestParam Map searMap,@PathVariable int page,@PathVariable int size){
        Page<Store> pageList = auditService.findPage(searMap, page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }


    /**
     * 根据主体编码查询当条数据的审核信息
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/findById/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id")
    })
    public Result findByCode(@PathVariable("id") Long id) {
        Merchant merchant = storeService.findByStoreId(id);
        return new Result(true, StatusCode.OK, "查询成功", merchant);
    }

    /**
     * 审核通过
     * @param
     * @return
     */
    @RequestMapping(value = "/pass/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id")
    })
    public Result updatePass(@PathVariable Long id){
        try {
           int count = auditService.updatePass(id);
            if (count >0){
                return new Result(true,StatusCode.STORE_status_YES,"审核通过");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
            return new Result(false,StatusCode.ERROR,"审核通过失败");
    }

    /**
     * 审核失败
     * @param id
     * @return
     */
    @RequestMapping(value = "/notPass/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id"),

    })
    public Result updateNotPass(@PathVariable Long id){
        try {
            int count = auditService.updateNotPass(id);
            if (count>0){
                return new Result(true,StatusCode.STORE_status_ERRO,"审核拒绝");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
         return new Result(false,StatusCode.ERROR,"审核拒绝失败");
    }


}
